<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class Admin extends Model
{
    //管理员表
    protected $table = "admin";
    public $timestamps = false;

    public static function getOneAdminInfo($id)
    {
        $res = self::where('id',$id)->first();
        return $res;
    }
    /**
     * 设置管理员状态 启用/禁用
     * @param $id
     * @param $param
     * @return mixed
     */
    public static function setStatus($id,$param)
    {
        if($param == 0){
            $res = self::where('id',$id)->update(['status'=>0]);
        }else{
            $res = self::where('id',$id)->update(['status'=>1]);
        }
        return $res;
    }

    /**
     * @param $id
     * @return mixed
     */
    public static function delAdmin($id)
    {
        $res = Admin::where('id',$id)->delete();
        return $res;
    }

    /**
     * 增加管理员
     * @param $data
     * @return mixed
     */
    public static function addAdmin($data)
    {
        $res = self::insert([
            'role_id'=>$data['role_id'],
            'rank'=>$data['rank'],
            'username'=>$data['username'],
            'password'=>Hash::make($data['password'])
        ]);
        return $res;
    }

    /**
     * 编辑管理员
     * @param $data
     * @return mixed
     */
    public static function updateAdmin($data)
    {
        $r['username'] = $data['username'];
        $r['rank'] = $data['rank'];
        $r['role_id'] = $data['role_id'];
        if($data['password']){
            $r['password'] = Hash::make($data['password']);
        }
        $res = self::where('id',$data['id'])->update($r);
        return $res;
    }
    /**
     * 验证添加管理员信息
     * @param $data
     * @return mixed
     */
    public static function addAdminValidate($data){
        $rules = array(
            'role_id'=>'required|integer',
            'rank'=>'required|integer',
            'password' => 'required|alpha_dash|between:1,12|confirmed',
            'password_confirmation' => 'required|alpha_dash|between:1,12',
        );
        $message = array(
            "required"             => ":attribute 不能为空",
            "between"      => ":attribute 长度必须在 :min 和 :max 之间",
            "alpha_dash"      => ":attribute 不能使用字母数字下划线之外的字符",
            "confirmed"      => "两次密码不一样",
            "integer"      => ":attribute 必须是整数"
        );

        $attributes = array(
            "role_id" => '角色',
            "rank" => '权重',
            "password" => '密码',
            'password_confirmation' => '重复密码',
        );

        $validate = Validator::make($data,$rules,$message,$attributes);
        return $validate;
    }

    /**
     * 验证修改管理员信息
     * @param $data
     * @return mixed
     */
    public static function updateAdminValidate($data){
        $rules = array(
            'role_id'=>'required|integer',
            'rank'=>'required|integer',
            'password' => 'alpha_dash|between:1,12|confirmed',
            'password_confirmation' => 'alpha_dash|between:1,12',
        );
        $message = array(
            "required"             => ":attribute 不能为空",
            "between"      => ":attribute 长度必须在 :min 和 :max 之间",
            "alpha_dash"      => ":attribute 不能使用字母数字下划线之外的字符",
            "confirmed"      => "两次密码不一样",
            "integer"      => ":attribute 必须是整数"
        );

        $attributes = array(
            "role_id" => '角色',
            "rank" => '权重',
            "password" => '密码',
            'password_confirmation' => '重复密码',
        );

        $validate = Validator::make($data,$rules,$message,$attributes);
        return $validate;
    }
}
